﻿@model Himall.Model.HomeFloorInfo
@using System.Linq;

<div class="container">
    <ul class="nav nav-tabs-custom clearfix">
        <strong>楼层编辑</strong>
        <li><a href="homeFloor">管理</a></li>
        <li class="active"><a id="mode">新增</a></li>
        <em class="primary-btn">
            <span class="glyphicon glyphicon-question-sign"></span>
            <div class="primary">
                <h3>温馨提示：</h3>
                <span>*</span><p>可通过可视化编辑设置商城首页</p>
            </div>
        </em>
    </ul>



    <form class="form-horizontal" role="form">
        <div class="form-group">
            <label for="" class="col-sm-2 control-label">楼层名称：</label>
            <div class="col-sm-3">
                <input type="text" class="form-control input-sm" maxlength="4" id="floorName" value="@Model.FloorName">
            </div>
            <div class="col-sm-5"><span class="help-default">最长4个字</span></div>
        </div>
        @*<div class="form-group">
            <label for="" class="col-sm-2 control-label">选择分类：</label>
            <div class="col-sm-10">
                <div class="checkbox-group">
                    @{var selectedCategoryIds =Model.FloorCategoryInfo.Select(item=>item.CategoryId);}
                    @foreach (var category in @ViewBag.TopLevelCategories)
                    {
                        long categoryId = category.Id;
                          <label class="checkbox-inline"><input type="checkbox" name="category" value="@category.Id" @(selectedCategoryIds.Contains(categoryId) ? "checked" : "")>@category.Name</label>
                    }
                </div>
            </div>
        </div>*@
        <input type="hidden" name="id" value="@Model.Id" />
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <a href="javascript:;" class="btn btn-primary" id="saveBtn">下一步</a>
                <a class="btn btn-warning ml15" id="floor-edit-next" style="display:none;">保存并进入模块编辑</a>
            </div>
        </div>

    </form>
</div>


<script>

    $(function () {

        checkEditMode();
        bindSaveBtnClickEvent();

    });

    function checkEditMode() {
        var id = $('input[name="id"]').val();
        id = parseInt(id);
        if (id) {
            //编辑模式
            $('#mode').html('编辑');
            $('#saveBtn').html('保存');
            $('#floor-edit-next').show();
            bindSaveAndNextClickEvent();
        }

    }

    function bindSaveAndNextClickEvent() {
        var id = $('input[name="id"]').val();
        $('#floor-edit-next').click(function () {
            save(function () {
                location.href = 'AddHomeFloorDetail?id=' + id;
            });
        });
    }

    function bindSaveBtnClickEvent() {
        var id = $('input[name="id"]').val();
        id = parseInt(id);
        $('#saveBtn').click(function () {
            var func = null;
            if (id) {
                //编辑模式
                func = function () {
                    $.dialog.tips('保存成功！', function () {
                        location.href = 'HomeFloor';
                    })
                }
            }
            else {
                func = function (id) { location.href = 'AddHomeFloorDetail?id=' + id };
            }
            save(func);
        });
    }


    function save(callBack) {
        var floorName = $.trim($('#floorName').val());
        if (!floorName) {
            $.dialog.tips('请填写楼层名称');
            $('#floorName').focus();
            return;
        }

        var categoryIds = [];
        $.each($('input[name="category"]:checked'), function () {
            categoryIds.push($(this).val());
        });
        if (categoryIds.length == 0) {
            $.dialog.tips('请至少选择一个分类');
            return;
        }

        var id = $('input[name="id"]').val();
        id = parseInt(id);
        var loading = showLoading();
        $.post('SaveHomeFloorBasicInfo',
            { id: id, name: floorName, categoryIds: categoryIds.toString() },
            function (result) {
                loading.close();
                if (result.success) {
                    callBack && callBack(result.id);
                }
                else {
                    $.dialog.errorTips('保存失败!' + result.msg);
                }
            });

    }


</script>